<?php
/**
 * @Author: lianlianzan(13040@qq.com)
 * @Date:   2018-08-18 16:40:16
 * @Last Modified by:   zhaozilong
 * @Last Modified time: 2018-12-26 16:17:46
 */

namespace app\common\model;

class User extends BaseNew {
	// 设置数据表（不含前缀）
	protected $name = '';

	// 定义时间戳字段名
	protected $createTime = '';
	protected $updateTime = '';

	// 自动完成
	protected $auto       = ['last_login_ip'];
	protected $insert     = ['register_ip'];

	public function setRegisterIpAttr($value) {
		return request()->ip();
	}

	// public function setPasswordAttr($value) {
	// 	if (!empty($value)) {
	// 		return sp_password($value); //encryptAdmin
	// 	}
	// }

	public function getStatusTextAttr($value,$data) {
		$status = [ 1 => '正常', -1 => '删除', 0 => '禁用', 2 => '待审核', 3 => '草稿'];
		return isset($status[$data['status']]) ? $status[$data['status']] : '未知';
	}

	public function getSexTextAttr($value,$data) {
		$sex = [ 0 => '保密', 1 => '男', 2 => '女'];
		return isset($sex[$data['sex']]) ? $sex[$data['sex']] : '未知';
	}

	/**
	 * 获取注册的时间戳
	 * @param  [type] $value [description]
	 * @param  [type] $data [description]
	 * @return [type] [description]
	 * @date   2018-02-28
	 * @author 心云间、凝听 <981248356@qq.com>
	 */
	// public function getRegTimestampAttr($value,$data) {
	// 	$timestamp = $data['reg_time'];
	// 	//判断是否是时间戳
	// 	// if(strtotime(date('m-d-Y H:i:s',$timestamp)) != $timestamp) {
	// 	//     $timestamp = strtotime($timestamp);
	// 	// }

	// 	return $timestamp;
	// }

	public function getListByUids($ids) {
		$map = array(
			'id'	=> array('in', implode(',', $ids)),
		);
		$data = $this->where($map)->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	public function countSearch($map) {
		return $this->where($map)->count();
	}

	public function search($map, $limit) {
		$data = $this->where($map)->order('create_time desc')->limit($limit)->select();
		$data = $data->toArray(); //转换为数组
		return $this->parseSearch($data);
	}

	protected function parseSearch($data) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('isopen_config');

		$list = $shids = array();
		foreach ($data as $key => $value) {
			//通用解析
			//$value['picurl'] = $value['picurl'] ? $ftpWeb.$value['picurl'] : '';
			//$value['create_time_show'] = $value['create_time'] ? date("Y-m-d H:i:s", $value['create_time']) : '';
			//$value['status_show'] = isset($value['status']) ? $isopen_config[$value['status']] : '';
			//通用解析

			$value['create_time_show'] = $value['create_time'] ? date("Y-m-d H:i:s", $value['create_time']) : '';
			$value['last_login_time_show'] = $value['last_login_time'] ? date("Y-m-d H:i:s", $value['last_login_time']) : '';
			$value['status_show'] = isset($value['status']) ? $isopen_config[$value['status']] : '';

			if ($value['openid']) { //微信用户

			} else { //手机用户
				$value['avatar'] = $value['avatar'] ? $ftpWeb.$value['avatar'] : '';
			}

			$list[$value['id']] = $value;
			$shids[$value['shid']] = $value['shid'];
		}
		return array($list, $shids);
	}

	protected function parseInfo($info) {
		$ftpWeb = config('app.ftp_web');
		$isopen_config = config('isopen_config');

		//通用解析
		//$info['picurl'] = $info['picurl'] ? $ftpWeb.$info['picurl'] : '';
		//$info['create_time_show'] = $info['create_time'] ? date("Y-m-d H:i:s", $info['create_time']) : '';
		//$info['status_show'] = isset($info['status']) ? $isopen_config[$info['status']] : '';
		//通用解析

		if ($info['openid']) { //微信用户

		} else { //手机用户
			$info['avatar'] = $info['avatar'] ? $ftpWeb.$info['avatar'] : '';
		}

		return $info;
	}

	public function getInfoByOpenid($openid) {
		$map = array(
			'openid'	=> $openid,
		);
		$info = $this->where($map)->find();
		//print_r($this->getLastSql());
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function getInfoByUnionid($unionid) {
		$map = array(
			'unionid'	=> $unionid,
		);
		$info = $this->where($map)->find();
		//print_r($this->getLastSql());
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function getInfoByUsername($username) {
		$map = array(
			'username'     => $username,
		);
		$info = $this->where($map)->find();
		//echo $this->getLastSql();exit;
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

	public function getInfoByMobile($mobile) {
		$map = array(
			'mobile'	=> $mobile,
		);
		$info = $this->where($map)->find();
		//print_r($this->getLastSql());
		return $info ? $this->parseInfo($info->toArray()) : []; //转换为数组并解析
	}

}